package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseNetworkDialogFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.entity.CustomerDialog;
import com.isunland.managesystem.utils.LogUtil;
import java.io.Serializable;

public class RequisitionListSearchFragment extends Fragment implements OnClickListener {

  private static final int REQUEST_ACQUIRE_VACATIONTYPE = 0;
  private static final int REQUEST_ACQUIRE_HOLYDATASTATUS = 1;

  public static final String EXTRA_DATASATUS = "com.isunland.managesystem.ui.EXTRA_DATASATUS";
  public static final String EXTRA_VACATION_TYPE =
      "com.isunland.managesystem.ui.EXTRA_VACATION_TYPE";

  private TextView mHolidayType_requisition; // 假期类型
  private TextView mDataSatusHoly_requisition;// 数据状态
  private CustomerDialog mDataStatus;
  private CustomerDialog mVacatioType;

  @Override public void onCreate(Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    ActionBar actionBar = ((BaseVolleyActivity) getActivity()).getSupportActionBar();
    if (actionBar != null) {

      if (NavUtils.getParentActivityName(getActivity()) != null) {
        actionBar.setDisplayHomeAsUpEnabled(true);
      }
      actionBar.setTitle(R.string.sift);
    }
    mDataStatus = (CustomerDialog) getArguments().getSerializable(EXTRA_DATASATUS);
    mVacatioType = (CustomerDialog) getArguments().getSerializable(EXTRA_VACATION_TYPE);
    LogUtil.i("mDataStatus==" + mDataStatus + ",,mVacatioType==" + mVacatioType);
    super.onCreate(savedInstanceState);
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_requisition_search, container, false);
    mHolidayType_requisition = (TextView) view.findViewById(R.id.tv_holidayType_requisition);
    mDataSatusHoly_requisition = (TextView) view.findViewById(R.id.tv_dataSatusHoly_requisition);

    mHolidayType_requisition.setText(
        mVacatioType == null ? getString(R.string.all) : mVacatioType.getName());
    mDataSatusHoly_requisition.setText(
        mDataStatus == null ? getString(R.string.all) : mDataStatus.getName());

    mHolidayType_requisition.setOnClickListener(this);
    mDataSatusHoly_requisition.setOnClickListener(this);
    return view;
  }

  @Override public void onClick(View v) {
    switch (v.getId()) {
      // 假期类型
      case R.id.tv_holidayType_requisition:
        showDialog(REQUEST_ACQUIRE_VACATIONTYPE);
        break;
      // 数据状态
      case R.id.tv_dataSatusHoly_requisition:
        showDialog(REQUEST_ACQUIRE_HOLYDATASTATUS);
        break;

      default:
        break;
    }
  }

  /**
   * 显示时间对话框
   *
   * @param requestCode 请求码
   */
  public void showDialog(int requestCode) {
    DialogFragment dialog = null;

    switch (requestCode) {
      // 假期类型
      case REQUEST_ACQUIRE_VACATIONTYPE:
        dialog = new RequisitionChooseDialogFragment();
        break;
      // 数据状态
      case REQUEST_ACQUIRE_HOLYDATASTATUS:
        dialog = new RequisitionDataStatusDialogFragment();
        break;

      default:
        break;
    }
    FragmentManager fm = getActivity().getSupportFragmentManager();
    dialog.setTargetFragment(RequisitionListSearchFragment.this, requestCode);
    dialog.show(fm, "");
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode != Activity.RESULT_OK) {
      return;
    }

    if (requestCode == REQUEST_ACQUIRE_VACATIONTYPE && data != null) {
      mVacatioType =
          (CustomerDialog) data.getSerializableExtra(BaseNetworkDialogFragment.EXTRA_VALUE);

      mHolidayType_requisition.setText(mVacatioType.getName());
    }
    if (requestCode == REQUEST_ACQUIRE_HOLYDATASTATUS && data != null) {
      mDataStatus = (CustomerDialog) data.getSerializableExtra(
          RequisitionDataStatusDialogFragment.EXTRA_DATA_STATUS);

      mDataSatusHoly_requisition.setText(mDataStatus.getName());
    }

    super.onActivityResult(requestCode, resultCode, data);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      /**
       * 响应返回按钮
       */
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;

      case R.id.menu_item_confirm:
        Intent intent = new Intent();
        intent.putExtra(EXTRA_DATASATUS, mDataStatus);
        intent.putExtra(EXTRA_VACATION_TYPE, mVacatioType);
        getActivity().setResult(Activity.RESULT_OK, intent);
        getActivity().finish();
        break;
      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  public static Fragment newInstance(Serializable status, Serializable vacatioType) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(EXTRA_DATASATUS, status);
    bundle.putSerializable(EXTRA_VACATION_TYPE, vacatioType);
    RequisitionListSearchFragment fragment = new RequisitionListSearchFragment();
    fragment.setArguments(bundle);
    return fragment;
  }
}
